package jgame.Admin;



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import jgame.StoryTeller.*;
import javax.swing.SwingConstants;

public class JGameFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel PANEL_TITLE;
	private JTextArea PANEL_TEXT;
	private JToolBar PANEL_OPTIONS;
	private int STATE;
	private JGameStorySet SET;
	private JGameStoryScene SCENE;
	private JGameContentReader CONTENT;
	
	/**
	 * Create the frame.
	 */
	public JGameFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 471, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);		
		
		PANEL_TITLE = new JLabel("New label");
		contentPane.add(PANEL_TITLE, BorderLayout.NORTH);
		
		PANEL_TEXT = new JTextArea();
		PANEL_TEXT.setEditable(false);
		PANEL_TEXT.setLineWrap(true);
		PANEL_TEXT.setWrapStyleWord(true);
		PANEL_TEXT.setPreferredSize(new Dimension(500,500));
		contentPane.add(PANEL_TEXT, BorderLayout.CENTER);
		
		PANEL_OPTIONS = new JToolBar();
		PANEL_OPTIONS.setOrientation(SwingConstants.VERTICAL);
		PANEL_OPTIONS.setFloatable(false);
		contentPane.add(PANEL_OPTIONS, BorderLayout.EAST);
	}
	
	public void setState(int state)
	{	
		STATE = state;
		setPanelContent();
	}
	
	public void setContent(JGameContentReader content)
	{	CONTENT = content;	}
	
	private void setPanelContent()
	{
		SET = CONTENT.getSet(STATE);
		SCENE = SET.getScene(STATE);
		PANEL_TITLE.setText(SET.getTitle());
		PANEL_TEXT.setText(SCENE.getText());
		PANEL_OPTIONS.removeAll();
		ArrayList<JGameStoryBranch> branches = SCENE.getBranches();
		
		for(int i = 0; i<branches.size(); i++)
			PANEL_OPTIONS.add(setOptions(branches.get(i)));
		
		contentPane.updateUI();
	}
	
	private JGameButton setOptions(JGameStoryBranch branch)
	{
		final JGameButton temp = new JGameButton(branch.getText(),branch.getTargetID());
		temp.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				setState(temp.getGameState());
			}
		});
		temp.setPreferredSize(new Dimension(120,40));
		temp.setBorderPainted(false);
		
		return temp;
	}
}
